package cn.com.nes.site.modules.sale;

import cn.com.nes.common.exception.BusinessException;
import cn.com.nes.site.modules.sale.enums.SaleTypeEnum;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

@Service
public class SaleServiceFactoty {

    @Resource
    private List<SaleHandle> saleHandles;

    private static final Map<SaleTypeEnum, SaleHandle> saleHandleMap = new ConcurrentHashMap<SaleTypeEnum, SaleHandle>();

    @PostConstruct
    public void init() {
        saleHandleMap.putAll(saleHandles.stream().collect(Collectors.toMap(SaleHandle::getSaleTypeEnum, s -> s)));
    }

    public SaleHandle getSaleHandle(SaleTypeEnum saleTypeEnum) {
        SaleHandle saleHandle = saleHandleMap.get(saleTypeEnum);
        if (saleHandle == null) {
            throw new BusinessException("未匹配的处理逻辑！");
        }
        return saleHandle;
    }
}
